package cn.zzmx.controller;


import cn.zzmx.entity.*;
import cn.zzmx.service.IUserService;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.RolesAllowed;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("user")
public class UserController {



    @Autowired
    private IUserService iUserService;


    @GetMapping("/queryUserAll")
    @RolesAllowed({"ADMIN","VIP"})
    @ResponseBody
    public ResultMsg<User> queryUserAll(Integer page, Integer limit){

        PageInfo<User> userPageInfo = iUserService.queryUserAll(page, limit);

        ResultMsg<User> msg = new ResultMsg<>();
        msg.setMsg("查询成功");
        msg.setCode(0);
        msg.setCount(userPageInfo.getTotal());
        msg.setData(userPageInfo.getList());

        return msg;
    }

    @GetMapping("queryUserDetails")
    @ResponseBody
    public ResultMsg<UserDetails> queryUserDetails(@Param("userId") String userId){


        List<UserDetails> list = new ArrayList<>();
        User user = iUserService.queryUserDetails(userId);
        List<Role> roleList = user.getRoleList();

        for (int i = 0; i < roleList.size(); i++) {
            list.add(new UserDetails(i+1,roleList.get(i).getTitle(),roleList.get(i).getRoleDesc(),"",1.1,0));

            List<Permission> permissionList = roleList.get(i).getPermissionList();
            for (int j = 0; j < permissionList.size(); j++) {
                list.add(new UserDetails(i+j+100,roleList.get(i).getTitle(),roleList.get(i).getRoleDesc(),"",1.1,i+1));
            }
        }

        ResultMsg<UserDetails> msg = new ResultMsg<>();
        msg.setCode(0);
        msg.setMsg("查询成功");
        msg.setData(list);
        return msg;



    }

    @PostMapping("/addUser")
    public ResultMsg<User> addUser(@RequestBody User user){
        return null;
    }



    @RequestMapping("/loginSuccess")
    public String loginSuccess(){
        return "redirect:/index.html";
    }

    @RequestMapping("/loginFailure")
    public String loginFailure(){
        return "redirect:/error.html";
    }


}
